﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectSequenceActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var ids = this.AsyncDialog("Id", request.Model, "Select").Split(',');

            var PortfolioId = Utility.UInt64Parse(this.AsyncDialog("Portfolio", "Portfolio"), 0);
            if (PortfolioId == 0)
            {
                return;

            }

            if (ids.Length > 0)
            {
                var sub = DataFactory.Instance().Portfolio(PortfolioId);

                var user = this.Context.Token.Identity();

                if (sub.project_id.HasValue)
                {
                    var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, user.Id.Value);

                    if (member != null)
                    {
                        switch (member.AuthType)
                        {
                            case WebAuthType.Admin:
                            case WebAuthType.User:
                                break;
                            default:
                                this.Prompt("您未有编辑此图文的权限");
                                break;
                        }

                    }
                    else
                    {
                        this.Prompt("您未有编辑此图文的权限");
                    }

                }
            }

            for (var i = 0; i < ids.Length; i++)
            {
                DataFactory.Instance().Put(new Subject { Id = Utility.UInt64Parse(ids[i], 0), Seq = i, portfolio_id = PortfolioId });

            }
            DataFactory.Instance().Put(new Portfolio { Id = PortfolioId, SubjectTotal = DataFactory.Instance().Subject(new Portfolio { Id = PortfolioId }).Length });
            response.Redirect(new WebMeta().Put("msg", ids.Length));


        }

    }
}